const CryptoJS = require("crypto-js")
const random = require('random-letters')

const status = {
  success: 0,
  error: 1,
}

module.exports = {
  // 数据接口响应格式
  httpJson(data = [], msg = '成功', code = status.success) {
    return {
      code,
      msg,
      data
    }
  },
  // 密码加密
  encryption(pwd, salt = null) {
    if(!salt) salt = random(4)

    return {
      password: CryptoJS.AES.encrypt(pwd, salt).toString(),
      salt
    }
  },
  checkPwd(pwd, salt, userPwd) {
    return CryptoJS.AES.decrypt(pwd, salt).toString(CryptoJS.enc.Utf8) == userPwd
  },
  status
}